package net.pws.common.persistence.hibernate3;

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * Session�ص��ӿڣ���Ҫ���ڴ���session�Ĳ������ںܶೡ�Ͼ�����ͬ��ִ���߼���
 * ֻ�ǵ���Sessoin�ķ�����һ����Java��֧��C#��Delegate�Ĺ��ܣ�ֻ��ͨ���������ݡ� ���߲���Templateģʽ��
 */
public interface SessionCallback {
    
    /**
     * ����session��ʵ����б���
     */
    public static final SessionCallback SAVE = new SessionCallback() {
        
        public Object callSession(Session session, Class clazz, Object entity) throws HibernateException {
            return session.save(entity);
        }
        
    };
    
    /**
     * ����session��ʵ����б��棬�����ˢ��ʵ��״̬����������ݿ��ж�����insert trigger�ĳ���
     */
    public static final SessionCallback SAVE_REFRESH = new SessionCallback() {
        
        public Object callSession(Session session, Class clazz, Object entity) throws HibernateException {
            Object id = session.save(entity);
            session.refresh(entity);
            return id;
        }
        
    };
    
    /**
     * ����session��ʵ����и���
     */
    public static final SessionCallback UPDATE = new SessionCallback() {
        
        public Object callSession(Session session, Class clazz, Object entity) throws HibernateException {
            session.update(entity);
            return entity;
        }
        
    };
    
    /**
     * ����session��ʵ����и��£������ˢ��ʵ��״̬����������ݿ��ж�����update trigger�ĳ���
     */
    public static final SessionCallback UPDATE_REFRESH = new SessionCallback() {
        
        public Object callSession(Session session, Class clazz, Object entity) throws HibernateException {
            session.update(entity);
            session.refresh(entity);
            return entity;
        }
        
    };
    
    /**
     * ����sessionɾ��ʵ��
     */
    public static final SessionCallback DELETE = new SessionCallback() {
        
        public Object callSession(Session session, Class clazz, Object entity) throws HibernateException {
            session.delete(entity);
            return entity;
        }
        
    };
    
    /**
     * ����sessionɾ��ָ��id��Ӧ��ʵ��
     */
    public static final SessionCallback DELETE_BY_ID = new SessionCallback() {
        
        public Object callSession(Session session, Class clazz, Object entity) throws HibernateException {
            session.delete(session.load(clazz, (Serializable) entity));
            return entity;
        }
        
    };
    
    /**
     * session�ص�����
     * 
     * @param session
     * @param clazz
     * @param entity
     * @return
     * @throws HibernateException
     */
    public Object callSession(Session session, Class clazz, Object entity) throws HibernateException;
    
}
